﻿<%@ CODEPAGE=65001 %>
<%
'///////////////////////////////////////////////////////////////////////////////
'// 插件应用:    Z-Blog 2.0 其它版本未知
'// 插件制作:    狼的旋律(http://www.wilf.cn) / zsx(http://www.zsxsoft.com)
'// 备    注:    WindsPhoto图片上传
'// 最后修改:
'// 最后版本:
'///////////////////////////////////////////////////////////////////////////////
%>
<% Option Explicit %>
<% 'On Error Resume Next %>
<% Response.Charset="UTF-8" %>
<% Response.Buffer=True %>
<!-- #include file="../../../zb_system/admin/ueditor/asp/aspincludefile.asp"-->
<%
Call System_Initialize()
Call WindsPhoto_Initialize()

Call CheckReference("")

If BlogUser.Level>2 Then Call ShowError(6)
If CheckpluginState("windsphoto") = False Then Call ShowError(48)

Dim upload,formName,FileName,FileNamet,FileNamelen,FileNamet1,FilePath,FileExt,imgWidth,imgHeight
Dim name,itime,url,surl,zhuanti,category,resize,jpeg_width,automark,autoname,syn_tqq,photointro

Set upload=New UpLoadClass
upload.AutoSave=2
upload.Charset="UTF-8"
upload.maxsize=ZC_UPLOAD_FILESIZE
upload.FileType=Replace(ZC_UPLOAD_FILETYPE,"|","/")

upload.open
name = upload.Form("name")
itime = now()
url = upload.Form("url")
surl = upload.Form("surl")
zhuanti = upload.Form("zhuanti")
category = upload.Form("category")
resize = upload.Form("resize")
jpeg_width = CInt(upload.Form("jpeg_width"))
automark = upload.Form("automark")
autoname = upload.Form("autoname")
syn_tqq = upload.Form("syn_tqq")
photointro = upload.Form("photointro")

FilePath=WindsPhoto_GetUploadFolder
FilePath=BlogPath & Replace(FilePath,"/","\")
upload.savepath=FilePath

If url<>"" Then

	If InStr(url, "http") = 0 Or InStr(url, "http") = Null Then
		Call SetBlogHint_Custom("远程图片的话,你只能添加http开头的图片地址.</a>")
		Response.Redirect "admin_addphoto.asp?typeid=" & zhuanti
	End If

	If surl = "" Then surl = url
	If name = "" Then name = "未命名"

	objConn.Execute "insert into WindsPhoto_desktop ([name],[itime],zhuanti,jj,url,surl,hot,viewnums) values ('"&name&"','"&itime&"',"&zhuanti&",'"&photointro&"','"&url&"','"&surl&"','0','0')"
	'Call SetBlogHint_Custom("√ 添加远程图片成功.")
	Call SetBlogHint(True,True,False)

	'同步到微博
	If CheckpluginState("QQConnect") = True And syn_tqq = "1" Then Call WindsPhoto_syn_tqq

	Response.Redirect "admin_addphoto.asp?typeid="&zhuanti&"#manage"

Else

	Dim intTemp
	For intTemp=1 to Ubound(upload.FileItem) '列出所有上传了的文件
		formname=upload.FileItem(intTemp)
		FileExt = LCase(upload.form(formname&"_Ext"))
		If name = "" Then name = Replace(upload.form(formname&"_Name"), "."&upload.form(formname&"_Ext"), "")
		If FileExt<>"gif" And FileExt<>"jpg" And FileExt<>"jpeg" And FileExt<>"png" Then
			Response.Redirect "admin_addphoto.asp?typeid=" & zhuanti
			Response.End
		End If

		If autoname="1"  Then Call upload.Save(formname,0) Else Call upload.Save(formname,1)

		Select Case upload.form(formname&"_Err")
			case -1:Call SetBlogHint_Custom("上传没有开始")
			case 1
				Call SetBlogHint_Custom("图片文件太大,无法上传.你可以修改<a href='../../../zb_system/admin/edit_setting.asp'>上传文件的最大字节数</a>.")
				Response.Redirect "admin_addphoto.asp?typeid=" & zhuanti
			case 2
				Call SetBlogHint_Custom("文件因扩展名不符合而未被保存.")
				Response.Redirect "admin_addphoto.asp?typeid=" & zhuanti
			case 3
				Call SetBlogHint_Custom("文件因过大且不符合扩展名.")
				Response.Redirect "admin_addphoto.asp?typeid=" & zhuanti
			case 4: Call SetBlogHint_Custom("异常，不存在上传.")
			case 5: Call SetBlogHint_Custom("上传已经取消，请检查总上载数据是否小于 "&m_TotalSize&" 。")
		End Select

		Filename=FilePath & upload.form(formname)
		FileNamet=upload.form(formname)

		'ASPJPEG处理
		If WP_IF_ASPJPEG=True Then

			Dim Jpeg
			Dim x1,y1,x2,y2,nwidth,nheight
			Set Jpeg = Server.CreateObject("Persits.Jpeg")
			'如果aspjpeg版本大于1.9，保护Metadata
			If Jpeg.Version>= "1.9" Then Jpeg.PreserveMetadata = True
			'渐进式JPEG
			Jpeg.Progressive = True
			Jpeg.Open FileName
			'变更缩略图文件扩展名为jpg
			FileNamelen = Len(FileNamet) - 4
			FileNamet1 = FileNamet
			FileNamet = Left(FileNamet, FileNamelen) &".jpg"
			imgWidth = Jpeg.OriginalWidth
			imgHeight = Jpeg.OriginalHeight

			'缩放图片
			If resize <> "0" And imgWidth>jpeg_width Then
				Jpeg.PreserveAspectRatio = True
				Jpeg.width = jpeg_width
			End If
			Jpeg.Quality = 100
			'gif不保存成jpg
			If FileExt<>"gif" Then Jpeg.Save FileName

			If WP_THUMB_TYPE = 1 Then
				'缩略图处理，以长边保持比例进行缩放
				If imgWidth>= imgHeight And imgWidth>WP_SMALL_WIDTH Then
					Jpeg.Width = WP_SMALL_WIDTH
					Jpeg.Height = imgHeight / (imgWidth / WP_SMALL_WIDTH)
				End If
				If imgHeight>imgWidth And imgHeight>WP_SMALL_HEIGHT Then
					Jpeg.Height = WP_SMALL_HEIGHT
					Jpeg.Width = imgWidth / (imgHeight / WP_SMALL_HEIGHT)
				End If
			Else
				'以短边进行裁剪缩放
				If (imgWidth/imgHeight)>=(WP_SMALL_WIDTH/WP_SMALL_HEIGHT) Then
					nwidth=CInt((imgWidth/imgHeight)*WP_SMALL_HEIGHT)
					nheight=WP_SMALL_HEIGHT
					Jpeg.Width=nwidth
					Jpeg.Height=nheight
					x1=int((nwidth-WP_SMALL_WIDTH)/2):y1=0:x2=x1+WP_SMALL_WIDTH:y2=WP_SMALL_HEIGHT
				Else
					nwidth=WP_SMALL_WIDTH
					nheight=CInt((imgHeight/imgWidth)*WP_SMALL_WIDTH)
					Jpeg.Width=nwidth
					Jpeg.Height=nheight
					x1=0:y1=int((nheight-WP_SMALL_HEIGHT)/2):x2=WP_SMALL_WIDTH:y2=y1+WP_SMALL_HEIGHT
				End If
				Jpeg.Crop x1,y1,x2,y2
			End If
			'保存缩略图，并进行微度锐化
			Jpeg.Sharpen 1, 110
			Jpeg.Save FilePath & "small_" & FileNamet
			Jpeg.Close

			If automark<>"" And FileExt<>"gif" Then
				Dim TextWidth,PositionWidth,PositionHeight
				Jpeg.Open FileName
				'渐进式JPEG
				Jpeg.Progressive = True
				Jpeg.Quality = WP_JPEG_QUALITY
				If WP_WATERMARK_TYPE = "1" Then '图片水印
					Jpeg.Canvas.Font.Color = Replace(WP_JPEG_FONTCOLOR, "#", "&h") '字体颜色
					Jpeg.Canvas.Font.Family = WP_JPEG_FONTFAMILY '字体family
					Jpeg.Canvas.Font.Bold = WP_JPEG_FONTBOLD '是否粗体
					Jpeg.Canvas.Font.Italic = WP_JPEG_FONTITALIC '是否斜体
					Jpeg.Canvas.Font.Size = WP_JPEG_FONTSIZE '字体大小
					Jpeg.Canvas.Font.Quality = WP_JPEG_FONTQUALITY '输出文字质量
					TextWidth = Jpeg.Canvas.GetTextExtent(WP_WATERMARK_TEXT)
					Select Case WP_WATERMARK_WIDTH_POSITION
						Case "left"
							PositionWidth = 10
						Case "center"
							PositionWidth = (Jpeg.Width - TextWidth) / 2
						Case "right"
							PositionWidth = Jpeg.Width - TextWidth - 10
					End Select
					Select Case WP_WATERMARK_HEIGHT_POSITION
						Case "top"
							PositionHeight = 10
						Case "center"
							PositionHeight = (Jpeg.Height - 12) / 2
						Case "bottom"
							PositionHeight = Jpeg.Height - 12 - 10
					End Select
					Jpeg.Canvas.Print PositionWidth, PositionHeight, WP_WATERMARK_TEXT
					Jpeg.Save FileName
				ElseIf WP_WATERMARK_TYPE = "2" Then
					Dim Logo
					Set Logo = Server.CreateObject("Persits.Jpeg")
					'WP_WATERMARK_LOGO = Server.MapPath("/") & "\zb_users\plugin\windsphoto\" & WP_WATERMARK_LOGO
					Logo.Open Server.MapPath("/") & "\zb_users\plugin\windsphoto\" & WP_WATERMARK_LOGO
					Select Case WP_WATERMARK_WIDTH_POSITION
						Case "left"
							PositionWidth = 10
						Case "center"
							PositionWidth = (Jpeg.Width - Logo.Width) / 2
						Case "right"
							PositionWidth = Jpeg.Width - Logo.Width - 10
					End Select
					Select Case WP_WATERMARK_HEIGHT_POSITION
						Case "top"
							PositionHeight = 10
						Case "center"
							PositionHeight = (Jpeg.Height - Logo.Height) / 2
						Case "bottom"
							PositionHeight = Jpeg.Height - Logo.Height - 10
					End Select
					If Instr(WP_WATERMARK_LOGO,"png") Or Instr(WP_WATERMARK_LOGO,"gif") Then
						If Jpeg.Version >= "1.8" Then Jpeg.Canvas.DrawPNG PositionWidth, PositionHeight, Server.MapPath("/") & "\zb_users\plugin\windsphoto\" & WP_WATERMARK_LOGO
					Else
						Jpeg.Canvas.DrawImage PositionWidth, PositionHeight, Logo, WP_WATERMARK_ALPHA
					End If
					Jpeg.Save FileName
					Set Logo = Nothing
				End If
			End If
			Set Jpeg = Nothing

		End If

		If WP_IF_ASPJPEG=True Then
			'带缩略图的URL路径
			If WP_UPLOAD_DIRBY = 1 Then
				url = WP_UPLOAD_DIR & "/" & Year(GetTime(Now()))&Month(GetTime(Now())) & "/" & FileNamet1
				surl = WP_UPLOAD_DIR & "/" & Year(GetTime(Now()))&Month(GetTime(Now())) & "/small_" & FileNamet
			ElseIf WP_UPLOAD_DIRBY = 2 Then
				url = WP_UPLOAD_DIR & "/" & zhuanti & "/" & FileNamet1
				surl = WP_UPLOAD_DIR & "/" & zhuanti & "/small_" & FileNamet
			Else
				url = WP_UPLOAD_DIR & "/" & FileNamet1
				surl = WP_UPLOAD_DIR & "/small_" & FileNamet
			End If
		Else
			'不带缩略图的URL路径
			If WP_UPLOAD_DIRBY = 1 Then
				url = WP_UPLOAD_DIR & "/" & Year(GetTime(Now()))&Month(GetTime(Now())) & "/" & FileNamet
				surl = WP_UPLOAD_DIR & "/" & Year(GetTime(Now()))&Month(GetTime(Now())) & "/" & FileNamet
			ElseIf WP_UPLOAD_DIRBY = 2 Then
				url = WP_UPLOAD_DIR & "/" & zhuanti & "/" & FileNamet
				surl = WP_UPLOAD_DIR & "/" & zhuanti & "/" & FileNamet
			Else
				url = WP_UPLOAD_DIR & "/" & FileNamet
				surl = WP_UPLOAD_DIR & "/" & FileNamet
			End If
		End If

		'写入数据库
		objConn.Execute "insert into WindsPhoto_desktop ([name],[itime],zhuanti,jj,url,surl,hot,viewnums) values ('"&name&"','"&itime&"',"&zhuanti&",'"&photointro&"','"&url&"','"&surl&"','0','0')"

		'If upload.form(formname&"_Err")=0 Then SetBlogHint_Custom(upload.form(formname&"_Name")&"上传成功.")
		If upload.form(formname&"_Err")=0 Then Call SetBlogHint(True,True,False)

		'同步到微博
		If CheckpluginState("QQConnect") = True And syn_tqq = "1" Then Call WindsPhoto_syn_tqq
	Next
	Set upload = Nothing

End If

Call System_Terminate()
%>